package com.briup.estore.web.servlet;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.Category;
import com.briup.estore.bean.extend.BookExtend;
import com.briup.estore.service.BookService;
import com.briup.estore.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/viewBook")
public class ViewBookServlet extends HttpServlet {

    private BookService bookService;
    {
        bookService = new BookServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、获取页面（index.jsp）传过来的数据（书籍id）
        String book_id = req.getParameter("book_id");
        // 2、调用service查询
        BookExtend thisBook = bookService.findBookAndCategoryById(Integer.parseInt(book_id));
        Category category = thisBook.getCategory();
        System.out.println(category);
        // 保存到session中
        HttpSession session = req.getSession();
        session.setAttribute("thisBook",thisBook);
        // 3、跳转页面
        req.getRequestDispatcher("/WEB-INF/jsp/viewBook.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
